package rules.tree;

import org.jdom.Element;
//import org.w3c.dom.*;

public class Rule {
	private Element el;
        private Head h;
        private Body b;
	
	public Rule(Element rule){
		this.el = rule;
                h = new Head(rule.getChild("head"));
                b = new Body(rule.getChild("body"));
	}

        public Rule(){
		el = new Element("mappingRule");                
	}
	
	public String getID(){
		return el.getAttributeValue("id");
	}

        public void setID(String id){
		el.setAttribute("id",id);
	}

        public boolean getHasTargetRestriction(){
		String value = el.getAttributeValue("hasTargetRestriction");
                if(value.equals("true"))return true;
                else return false;
        }

        public void setHasTargetRestriction(Boolean value){
		el.setAttribute("id",value.toString());
	}
        
	public Head getHead(){
		return h;
	}

        public void setHead(Head h){
                if(h.getAtom() instanceof ClassAtom)el.setAttribute("type","classMapping");
                if(h.getAtom() instanceof PropertyAtom)el.setAttribute("type", "propertyMapping");
                this.h = h;
                /*Element e;
                if((e=el.getChild("head"))!=null){
                    el.removeChild("head");
                }*/
                el.addContent(h.getElement());
	}

	public Body getBody(){
		return b;
	}
	
	public void setBody(Body b){
		this.b = b;
               /* Element e;
                if((e=el.getChild("body"))!=null){
                    el.removeChild("body");
                }*/
                el.addContent(b.getBody());
	}

        public Element getElement(){
            return el;
        }
}
